package com.zzld.modules.autoinn.action;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.github.abel533.entity.Example;
import com.zzld.modules.autoinn.entity.Expense;
import com.zzld.modules.autoinn.service.IExpenseService;

@Controller
@RequestMapping("/autoinn/xiao")
public class XiaoliangController {

	@Autowired
	private IExpenseService expenseService;

	private String inurl = "/modules/autoinn/xiao/xiaoindex";
	
	private String p1url="/modules/autoinn/portal/p1";
	
	private String p2url="/modules/autoinn/portal/p2";

	private String datime = datea();
	
	/**
	 *  p1 页面跳转
	 * @Title: p1pre
	 * @Description: TODO
	 * @param:@return
	 * @return: String
	 * @throws 
	 */
	@RequestMapping("p1pre")
	public String p1pre(Model model){
		double shiprice = 0;
		double yfprice = 0;
		try {
			shiprice = Double.valueOf(expenseService.selectbyprice("price"));
			yfprice = Double.valueOf(expenseService.selectbyyfprice());
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		Map<String, Object> ms = xiao(0);
		int s = ms.size();
		String yue = ""; //月份
		String sji = ""; //实际金额
		String yfsj = ""; //应付金额
		for (int p = 1; p <= xiao(1).size(); p++) {
			if (!xiao(1).containsKey(p)) {
				yfsj += xiao(1).get(String.valueOf(p)) + ",";
			}
		}
		for (int q = 1; q <= s; q++) {
			if (!ms.containsKey(q)) {
				yue += "'" + (q) + "月',";
			}
		}
		
		for (int qo = 1; qo <= s; qo++) {
			if (!ms.containsKey(qo)) {
				sji += ms.get(String.valueOf(qo)) + ",";
			}
		}
		model.addAttribute("yfsj", yfsj.subSequence(0, yfsj.length() - 1));
		model.addAttribute("sji", sji.substring(0, sji.length() - 1));
		model.addAttribute("yue", yue.subSequence(0, yue.length() - 1));
		model.addAttribute("sf", shiprice);
		model.addAttribute("yf", yfprice);
		return p1url;
	}
	
	
	@RequestMapping("p2pre")
	public String p2pre(Model model){
		Example example = new Example(Expense.class);
		int qi = 0;
		try {
			qi = expenseService.selectCountByExample(example);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		Map<String, Object> ms = xiao(0);
		int s = ms.size();
		String yue = ""; //月份
		for (int q = 1; q <= s; q++) {
			if (!ms.containsKey(q)) {
				yue += "'" + (q) + "月',";
			}
		}
		
		Map<String, Object> mo = liang();
		String yueliang = "";
		for (int u = 1; u <= mo.size(); u++) {
			if (!mo.containsKey(u)) {
				yueliang += mo.get(String.valueOf(u)) + ",";
			}
		}
		model.addAttribute("yue", yue.subSequence(0, yue.length() - 1));
		model.addAttribute("yueliang", yueliang.substring(0, yueliang.length() - 1));
		model.addAttribute("liang", qi);
		
		return p2url;
	}
	

	@RequestMapping(method = RequestMethod.GET)
	public String list(Model model) {
		model.addAttribute("shu", xiao(0));
		model.addAttribute("jinprice", dateprice().get("jinprice"));
		model.addAttribute("jinyfprice", dateprice().get("jinyfprice"));
		model.addAttribute("jinliang", dateprice().get("jinliang"));
		return inurl;
	}

	private Map<String, Object> liang() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Expense> list = null;

		try {
			for (int i = 0; i < 12; i++) {
				Example e = new Example(Expense.class);
				e.createCriteria().andLike("innertime",
						"%" + datime + "-0" + (i + 1) + "%");
				list = expenseService.selectByExample(e);
				map.put(String.valueOf(i + 1), list.size());

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}


	/**
	 * 
	 * @Title: xiao 销售
	 * @Description: TODO
	 * @param:@param iq 1 取实际付款 0应付款 
	 * @param:@return 
	 * @return: Map<String,Object>
	 * @throws 
	 */
	private Map<String, Object> xiao(int iq) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Expense> list = null;
		double shi = 0;
		try {
			for (int i = 0; i < 12; i++) {
				Example e = new Example(Expense.class);
				e.createCriteria().andLike("innertime",
						"%" + datime + "-0" + (i + 1) + "%");
				list = expenseService.selectByExample(e);
				if (list.size() > 0) {
					shi = 0;
					for (int j = 0; j < list.size(); j++) {
						if (iq == 0) {
							shi = shi + list.get(j).getPrice();
						} else {
							shi = shi + (list.get(j).getYfprice()>0?list.get(j).getYfprice():0);
						}
					}

					map.put(String.valueOf(i + 1), String.valueOf(shi));
				} else {
					map.put(String.valueOf(i + 1), 0);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	// 今日销售情况
	public Map<String, Object> dateprice() {
		Map<String, Object> masp = new HashMap<String, Object>();
		List<Expense> list = null;
		double price = 0;
		double yfprice = 0;
		try {
			// 当前时间
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String datetime = sdf.format(new Date());

			Example e2 = new Example(Expense.class);
			e2.createCriteria().andLike("innertime", "%" + datetime + "%");
			list = expenseService.selectByExample(e2);
			if (list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					price = price + list.get(i).getPrice();
					
					yfprice = yfprice + (list.get(i).getYfprice()>0?list.get(i).getYfprice():0);
				}
				masp.put("jinprice", price);
				masp.put("jinyfprice", yfprice);
				masp.put("jinliang", list.size());
			} else {
				masp.put("jinprice", 0);
				masp.put("jinyfprice", 0);
				masp.put("jinliang", 0);
			}
		} catch (Exception e11) {
			e11.printStackTrace();
		}
		return masp;
	}

	public String datea() {
		Calendar ca = Calendar.getInstance();
		int year = ca.get(Calendar.YEAR);// 获取年份
		return String.valueOf(year);
	}
}
